package com.usian.wemedia.controller;

import com.sun.xml.bind.v2.model.core.ID;
import com.usian.apis.wemedia.WmNewsControllerApi;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmNewsVo;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    private WmNewsService wmNewsService;

    /****
     * 文章列表查询
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/list")
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findAll(dto);
    }

    /***
     * 文章保存 修改 草稿
     * @param wmNews
     * @return
     */
    @PostMapping("/submit")
    @Override
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNews) {
        if (wmNews.getStatus().equals(WmNews.Status.SUBMIT.getCode())){
            return wmNewsService.saveNews(wmNews,(short)1);
        }else {

            return wmNewsService.saveNews(wmNews,(short)0);
        }
    }

    /***
     * 根据文章id查询回显
     * @param newId
     * @return
     */
    @GetMapping("/one/{newId}")
    @Override
    public ResponseResult findWmNewsById( @PathVariable Integer newId) {
        return wmNewsService.findWmNewsById(newId);
    }

    /***
     * 根据id删除文章
     * @param id
     * @return
     */
    @GetMapping("/del_news/{id}")
    @Override
    public ResponseResult delNews(@PathVariable Integer id) {
        return wmNewsService.delNews(id);
    }

    /***
     * 上下架
     * @param dto
     * @return
     */
    @PostMapping("/down_or_up")
    @Override
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

    /***
     *
     * admin端审核 根据文章id查询
     * @param
     * @return
     */
    @Override
    @GetMapping("/findOne/{id}")
    public WmNews findById(@PathVariable Integer id) {
        return wmNewsService.getById(id);
    }

    @Override
    @PostMapping("/update")
    public ResponseResult update(@RequestBody WmNews wmNews) {
         wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @GetMapping("/findRelease")
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

    @Override
    @PostMapping("/findList")
    public PageResponseResult findList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findListAndPage(dto);
    }

    @Override
    @GetMapping("/find_news_vo/{id}")
    public WmNewsVo findWmNewsVo(@PathVariable Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }

}
